<template>
  <section>
    <!--工具条-->
    <toolbar :buttonList="buttonList" @callFunction="callFunction"></toolbar>
    <!--列表-->
    <el-pagination
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :page-size="page.PageSize"
      :total="page.PageCount"
      style="float: right"
    >
    </el-pagination>
    <el-table
      :data="userslist"
      highlight-current-row
      v-loading="listLoading"
      @row-click="selsChange"
      style="width: 100%"
      ref="mytable"
    >
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column label="序号" width="70px">
        <template slot-scope="scope">
          {{ (page.PageIndex - 1) * page.PageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="Name"
        label="真实姓名"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="UserName"
        label="用户名"
        width="150"
      ></el-table-column>
      <el-table-column label="超级管理员" width="">
        <template slot-scope="scope">
          {{ scope.row.IsSuper ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="是否禁用" width="">
        <template slot-scope="scope">
          <el-tag
            :type="!scope.row.IsEnabled ? 'success' : 'danger'"
            disable-transitions
            >{{ scope.row.IsEnabled ? "禁用" : "正常" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="roleNames" label="所属角色" width="200" sortable>
        <template slot-scope="scope">
          <el-tag
            type=""
            v-for="role in scope.row.RoleNames"
            v-bind:key="role"
            >{{ role }}</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column prop="Remark" label="备注" width="250">
      </el-table-column>
      <el-table-column
        :formatter="formatDate"
        prop="InsertTime"
        label="添加时间"
        width=""
      ></el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="page.PageSize"
        :total="page.PageCount"
        style="float: right"
      >
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog
      title="编辑"
      :visible.sync="addFormVisible"
      v-model="addFormVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="addFormModel"
        label-width="80px"
        :rules="addFormRules"
        ref="addFormModel"
      >
        <el-form-item label="真实姓名" prop="Name">
          <el-input
            v-model="addFormModel.Name"
            maxlength="10"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="RoleNames">
          <el-select
            multiple
            v-model="addFormModel.RoleIds"
            placeholder="请选择角色"
          >
            <!-- <el-option :key="0" :label="'未选择角色'" :value="0"></el-option> -->
            <el-option
              v-for="item in roles"
              :key="item.ID"
              :label="item.Title"
              :value="item.ID"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="登录名" prop="UserName">
          <el-input
            maxlength="10"
            v-model="addFormModel.UserName"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="PassWord">
          <el-input
            maxlength="20"
            v-model="addFormModel.PassWord"
            show-password
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="启用状态" prop="IsEnabled">
          <el-select v-model="addFormModel.IsEnabled" placeholder="请选择状态">
            <el-option label="禁用" :value="true"></el-option>
            <el-option label="启用" :value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号码" prop="Mobile">
          <el-input
            maxlength="20"
            v-model="addFormModel.Mobile"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="Remark">
          <el-input
            maxlength="500"
            type="textarea"
            :rows="4"
            v-model="addFormModel.Remark"
            auto-complete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="SaveSubmit"
          :loading="addLoading"
          >提交</el-button
        >
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from "../../../util/date";
import {
  getUserListPage,
  GetRoleListEnabled,
  removeUser,
  saveUser,
} from "../../api/api";
import { getButtonList } from "../../promissionRouter";
import Toolbar from "../../components/Toolbar";
export default {
  components: { Toolbar },
  data() {
    return {
      filters: {
        name: "",
      },
      userslist: [],
      roles: [],
      buttonList: [],
      page: {
        PageSize: 10,
        PageIndex: 1,
        PageCount: 0,
        Where: "",
      },
      listLoading: false,
      select: {}, //列表选中列
      addFormVisible: false,
      addLoading: false,
      addFormRules: {
        UserName: [
          { required: true, message: "请输入登录名", trigger: "blur" },
        ],
        PassWord: [
          { required: true, message: "请输入登录名", trigger: "blur" },
        ],
        Name: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        Mobile: [
          { required: true, message: "请填写手机号码", trigger: "blur" },
        ],
      },
      //编辑界面数据
      addFormModel: {
        Name: "",
        UserName: "",
        Mobile: "",
        PassWord: "",
        RoleIds: [],
      },
    };
  },
  methods: {
    callFunction(item) {
      this.filters = {
        name: item.search,
      };
      if(item.Func=='search')
      {
        this.page.PageIndex=1;
      }
      this[item.Func].apply(this, item);
    },
    formatDate: function (row, column) {
      return !row.InsertTime || row.InsertTime == ""
        ? ""
        : util.formatDate.format(new Date(row.InsertTime), "yyyy-MM-dd");
    },
    handleCurrentChange(val) {
      this.page.PageIndex = val;
      this.search();
    },
    //获取用户列表
    search() {
      this.page.Where = this.filters.name;
      this.listLoading = true;
      getUserListPage(this.page).then((res) => {
        this.page.PageCount = res.Data.PageCount;
        this.userslist = res.Data.Data;
        this.listLoading = false;
      });
    },
    //删除
    remove() {
      var rows = this.$refs.mytable.selection;
      if (rows.length == 0) {
        this.$message({
          message: "请选择要删除的数据！",
          type: "error",
        });
        return;
      }
      var arrayIds = [];
      var arrayNames = [];
      var userIds = rows.map((c) => {
        arrayIds.push(c.ID);
        arrayNames.push(c.Name);
        return c.ID;
      });

      this.$confirm("确认删除【" + arrayNames.join(",") + "】记录吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          this.listLoading = true;
          var ids=arrayIds.join(",");
           console.log(ids);
          removeUser(ids).then((res) => {
            this.listLoading = false;
            if (res.data.IsSuccess) {
              this.$message({
                message: res.data.Message,
                type: "success",
              });
            } else {
              this.$message({
                message: res.data.Message,
                type: "error",
              });
            }
            this.search();
          });
        })
        .catch(() => {});
    },
    //显示编辑界面
    update() {
      if (!this.select.hasOwnProperty("ID")) {
        this.$message({
          message: "请选择要编辑的一行数据！",
          type: "error",
        });

        return;
      }
      this.addFormVisible = true;
      this.addFormModel = Object.assign({}, this.select);
    },
    //显示新增界面
    insert() {
      this.addFormVisible = true;
      this.addFormModel = {};
    },
    //编辑
    SaveSubmit: function () {
      this.$refs.addFormModel.validate((valid) => {
        if (this.addFormModel.RoleIds.length == 0) {
          this.$message({
            message: "请选择角色",
            type: "error",
          });
          return;
        }

        if (valid) {
          //this.addLoading = true;
          let para = Object.assign({}, this.addFormModel);
          saveUser(para).then((res) => {
            if (util.isEmt.format(res)) {
              this.addLoading = false;
              return;
            }
            if (res.data.IsSuccess) {
              this.addLoading = false;
              //NProgress.done();
              this.$message({
                message: res.data.Message,
                type: "success",
              });
              this.$refs["addFormModel"].resetFields();
              this.addFormVisible = false;
              this.search();
            } else {
              this.$message({
                message: res.data.Message,
                type: "error",
              });
            }
          });
        }
      });
    },
    selsChange: function (row) {
      this.select = row;
      this.$refs.mytable.clearSelection();
      this.$refs.mytable.toggleRowSelection(row);
    },
    getRole() {
      GetRoleListEnabled({}).then((res) => {
        this.roles = res.Data;
      });
    },
  },
  mounted() {
    this.search();
    this.getRole();
    let routers = window.localStorage.router
      ? JSON.parse(window.localStorage.router)
      : [];
    this.buttonList = getButtonList(this.$route.path, routers);
  },
};
</script>

<style scoped>
</style>
